package org.farng.mp3.id3;

import org.farng.mp3.InvalidTagException;

import java.io.IOException;
import java.io.RandomAccessFile;

import java.util.Arrays;


/**
 * Title: Description: Copyright:    Copyright (c) 2002 Company:
 *
 * @author Eric Farng
 * @version $Revision: 1.3 $
 */
public class FrameBodyUnsupported extends AbstractID3v2FrameBody {
    /** DOCUMENT ME! */
    private String identifier = "";

    /** DOCUMENT ME! */
    private byte[] value;

    /**
     * Creates a new FrameBodyUnsupported object.
     *
     * @param value DOCUMENT ME!
     */
    public FrameBodyUnsupported(byte[] value) {
        this.value = value;
    }

    public FrameBodyUnsupported(FrameBodyUnsupported copyObject) {
        super(copyObject);
        this.identifier = new String(copyObject.identifier);
        this.value      = (byte[]) copyObject.value.clone();
    }

    /**
     * Creates a new FrameBodyUnsupported object.
     *
     * @param file DOCUMENT ME!
     *
     * @throws IOException DOCUMENT ME!
     * @throws InvalidTagException DOCUMENT ME!
     */
    public FrameBodyUnsupported(RandomAccessFile file)
                         throws IOException, InvalidTagException {
        this.read(file);
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public String getIdentifier() {
        return identifier;
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public int getSize() {
        int size = 0;

        if (value != null) {
            size += value.length;
        }

        return size;
    }

    /**
     * DOCUMENT ME!
     *
     * @param obj DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public boolean isSubsetOf(Object obj) {
        if ((obj instanceof FrameBodyUnsupported) == false) {
            return false;
        }

        FrameBodyUnsupported object = (FrameBodyUnsupported) obj;

        String               subset = new String((byte[]) this.value);
        String               superset = new String((byte[]) object.value);

        if (superset.indexOf(subset) < 0) {
            return false;
        }

        return super.isSubsetOf(obj);
    }

    /**
     * DOCUMENT ME!
     *
     * @param obj DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public boolean equals(Object obj) {
        if ((obj instanceof FrameBodyUnsupported) == false) {
            return false;
        }

        FrameBodyUnsupported object = (FrameBodyUnsupported) obj;

        if (this.identifier.equals(object.identifier) == false) {
            return false;
        }

        if (Arrays.equals(this.value, object.value) == false) {
            return false;
        }

        return super.equals(obj);
    }

    /**
     * DOCUMENT ME!
     *
     * @param file DOCUMENT ME!
     *
     * @throws IOException DOCUMENT ME!
     * @throws InvalidTagException DOCUMENT ME!
     */
    public void read(RandomAccessFile file)
              throws IOException, InvalidTagException {
        int    size;
        byte[] buffer;

        if (use6ByteHeader()) {
            // go back and read the 3 byte unsupported identifier;
            file.seek(file.getFilePointer() - 3);
            buffer = new byte[3];
            file.read(buffer);
            identifier = new String(buffer, 0, 3);
        } else {
            // go back and read the 4 byte unsupported identifier;
            file.seek(file.getFilePointer() - 4);
            buffer = new byte[4];
            file.read(buffer);
            identifier = new String(buffer);
        }

        //System.out.println("Frame type unsupported : " + identifier);
        size = readHeader(file);

        // read the data
        value = new byte[size];
        file.read(value);
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public String toString() {
        return "??" + getIdentifier() + " : " + (new String(value));
    }

    /**
     * DOCUMENT ME!
     *
     * @param file DOCUMENT ME!
     *
     * @throws IOException DOCUMENT ME!
     */
    public void write(RandomAccessFile file)
               throws IOException {
        writeHeader(file, this.getSize());
        file.write(value);
    }
}